/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package leeranotacionesdataclassg;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.leeranotacionesdataclassg.anotaciones.Columna;
import org.leeranotacionesdataclassg.anotaciones.Relaciones;
import org.leeranotacionesdataclassg.anotaciones.Table;
import org.leeranotacionesdataclassg.beans.Privilegios;

/**
 *
 * @author avbravo
 */
public class Lector {

    public  void LeerClaseDirectamente() {
        try {
            Class aClass = Privilegios.class;

            Annotation[] annotations = aClass.getAnnotations();
            /*
             * a nivel de clase
             */
            for (Annotation annotation : annotations) {
                if (annotation instanceof Table) {
                    Table myAnnotation = (Table) annotation;
                    System.out.println("tabla: " + myAnnotation.nombre());
                    //System.out.println("value: " + myAnnotation.catalogo());
                }
            }
            /*
             * Ahora los campos
             */
            System.out.println(" Columnas:");
            Field[] fields = aClass.getDeclaredFields();
            for (Field f : fields) {
                Annotation[] annotationsf = f.getDeclaredAnnotations();
                for (Annotation annotation : annotationsf) {
                    if (annotation instanceof Columna) {
                        Columna myAnnotation = (Columna) annotation;

                        System.out.print("nombre: " + myAnnotation.nombre());
                        System.out.print("  tipo: " + myAnnotation.tipo());
                        System.out.println("        ");
                    } else {
                        System.out.println("Relaciones:->");
                        if (annotation instanceof Relaciones) {
                            Relaciones myAnnotation = (Relaciones) annotation;

                            System.out.print("columna: " + myAnnotation.columna());
                            System.out.print("  tabla: " + myAnnotation.tabla());
                            System.out.print("  nombre relacion: " + myAnnotation.nombre_relacion());
                            System.out.println("        ");
                        }
                    }
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    
    
    public void LeerParametroClaseString(String cn) {
        try {
Class aClass = Class.forName(cn);
         //   Class aClass = t.getClass();

//            System.out.println(" " + t.getClass().getCanonicalName());
            if (aClass == null) {
                System.out.println("es null");
            }
            Annotation[] annotations = aClass.getAnnotations();
            /*
             * a nivel de clase
             */
            for (Annotation annotation : annotations) {
                if (annotation instanceof Table) {

                    Table myAnnotation = (Table) annotation;
                    System.out.println("tabla: " + myAnnotation.nombre());
                    //System.out.println("value: " + myAnnotation.catalogo());
                }
            }
            /*
             * Ahora los campos
             */
            System.out.println(" Columnas:-->");
            System.out.println("----------------");
            Field[] fields = aClass.getDeclaredFields();
            for (Field f : fields) {
                Annotation[] annotationsf = f.getDeclaredAnnotations();
                for (Annotation annotation : annotationsf) {
                    if (annotation instanceof Columna) {
                        Columna myAnnotation = (Columna) annotation;

//                        System.out.print("nombre: " + myAnnotation.nombre());
//                        System.out.print("  tipo: " + myAnnotation.tipo());
//                        System.out.println("        ");
                    } else {
                        System.out.println("Relaciones:-->");
                        if (annotation instanceof Relaciones) {
                            Relaciones myAnnotation = (Relaciones) annotation;

                            System.out.print("columna: " + myAnnotation.columna());
                            System.out.print("  tabla: " + myAnnotation.tabla());
                            System.out.print("  nombre relacion: " + myAnnotation.nombre_relacion());
                            System.out.println("        ");
                       ArrayList<String> list =     descomponerCadena(myAnnotation.columna(),",");
                       for(String l:list){
                           System.out.println(""+l);
                       }
                        }
                    }
                }
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    
     public static ArrayList<String> descomponerCadena(String texto, String separador) {
        ArrayList<String> lista = new ArrayList<String>();
        try {
            lista.removeAll(lista);
            StringTokenizer tk = new StringTokenizer(texto, separador); // Cambia aquí el separador
            while (tk.hasMoreTokens()) {

                lista.add(tk.nextToken());
            }
        } catch (Exception ex) {
            System.out.println("Error "+ex.getLocalizedMessage());
        }
        return lista;
    }
}
